package com.leo.blog.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.leo.blog.dao.BlogerDao;
import com.leo.blog.dto.BlogerExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.enums.BlogerStateEnum;
import com.leo.blog.exceptions.BlogerOperationException;
import com.leo.blog.service.BlogerService;
import com.leo.blog.util.PageCalculator;

@Service
public class BlogerServiceImpl implements BlogerService{
	@Autowired
	private BlogerDao blogerDao;

	@Override
	public BlogerExecution getBlogerById(int blogerId) {
		if(blogerId < 0) {
			return new BlogerExecution(BlogerStateEnum.NULL_BLOGER);
		}else {
			try {
				Bloger bloger = blogerDao.queryBlogerById(blogerId);
				if(bloger != null) {
					return new BlogerExecution(BlogerStateEnum.SUCCESS, bloger);
				}else {
					return new BlogerExecution(BlogerStateEnum.INNER_ERROR);
				}
			} catch (Exception e) {
				throw new BlogerOperationException("getBloger error: " + e.getMessage());
			}		
		}
	}

	@Override
	public BlogerExecution addBloger(Bloger bloger) throws BlogerOperationException {
		if(bloger != null) {
			String password = DigestUtils.md5DigestAsHex(bloger.getPassword().getBytes());
			
			bloger.setPassword(password);
			
			int effectedNum = blogerDao.insertBloger(bloger);
			if(effectedNum > 0) {
				return new BlogerExecution(BlogerStateEnum.SUCCESS);
			}else {
				return new BlogerExecution(BlogerStateEnum.OFFLINE);
			}
		}else {
			return new BlogerExecution(BlogerStateEnum.NULL_BLOGER);
		}
	}

	@Override
	public BlogerExecution addSelective(Bloger bloger) throws BlogerOperationException {
		if(bloger != null) {
			int effectedNum = blogerDao.insertSelective(bloger);
			if(effectedNum > 0) {
				return new BlogerExecution(BlogerStateEnum.SUCCESS);
			}else {
				return new BlogerExecution(BlogerStateEnum.OFFLINE);
			}
		}else {
			return new BlogerExecution(BlogerStateEnum.NULL_BLOGER);
		}
	}

	@Override
	public BlogerExecution updateBloger(Bloger bloger) {
		if(bloger != null) {
			int effectedNum = blogerDao.updateBloger(bloger);
			if(effectedNum > 0) {
				return new BlogerExecution(BlogerStateEnum.SUCCESS);
			}else {
				return new BlogerExecution(BlogerStateEnum.OFFLINE);
			}
		}else {
			return new BlogerExecution(BlogerStateEnum.NULL_BLOGER);
		}
	}

	@Override
	public BlogerExecution updateBlogerSelective(Bloger bloger) {
		if(bloger != null) {
			int effectedNum = blogerDao.updateBlogerSelective(bloger);
			if(effectedNum > 0) {
				return new BlogerExecution(BlogerStateEnum.SUCCESS);
			}else {
				return new BlogerExecution(BlogerStateEnum.OFFLINE);
			}
		}else {
			return new BlogerExecution(BlogerStateEnum.NULL_BLOGER);
		}
	}

	@Override
	public BlogerExecution deleteBlogerById(int blogerId) {
		if(blogerId > 0) {
			int effectedNum = blogerDao.deleteBlogerById(blogerId);
			if(effectedNum > 0) {
				return new BlogerExecution(BlogerStateEnum.SUCCESS);
			}else {
				return new BlogerExecution(BlogerStateEnum.OFFLINE);
			}
		}else {
			return new BlogerExecution(BlogerStateEnum.OFFLINE);
		}
	}

	@Override
	public BlogerExecution getUserByLoginName(String userName) {
		BlogerExecution be = new BlogerExecution();
		if(userName != null) {
			Bloger bloger = blogerDao.queryUserByLoginName(userName);
			be.setBloger(bloger);
			be.setState(BlogerStateEnum.SUCCESS.getState());
		}else {
			be.setState(BlogerStateEnum.OFFLINE.getState());//-1
		}
		return be;
	}

	@Override
	public BlogerExecution getBlogerList(Bloger blogerCondition, int pageIndex, int pageSize) {
		BlogerExecution be = new BlogerExecution();
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,待用dao层返回相关类型列表
		List<Bloger> blogerList = blogerDao.queryBlogerList(blogerCondition, rowIndex, pageSize);
		int count = blogerDao.queryBlogerListCount(blogerCondition);
		if(blogerList != null && count > 0) {
			be.setBlogerList(blogerList);
			be.setCount(count);
			be.setState(BlogerStateEnum.SUCCESS.getState());
		}else {
			be.setState(BlogerStateEnum.OFFLINE.getState());
		}	
		return be;
	}
}
